Creating a random string using regexp

I was wondering if anyone knew exactly how to use a regexp to create a random 25 character string using upper and lower case letters and numbers. Basically I am trying to figure out how to make a string that follows the DOORs password guidelines for a side project I am putting to gether for user password reset requests.

Im just exceptionally horrible at regexp


MrNiceGuy - Tue Nov 28 19:26:53 EST 2017

Re: Creating a random string using regexp
EHcnck - Tue Nov 28 20:51:45 EST 2017

string getRandompw() {
        int i = 0
        int x = 0
        b = ""

        for ( i= 0; i<32; i++ ) {
                while ( true ) {
                        x = random(123)
                        if ( x > 48 && x < 58 )  break //[0-9]
                        if ( x > 64 && x < 91 )  break //[a-z]
                        if ( x > 96 && x < 123 ) break //[A-Z]
                }
                b += charOf(x)
        }

        return(tempStringOf(b))
}

 

Re: Creating a random string using regexp
EHcnck - Tue Nov 28 21:14:46 EST 2017

// User password reset

/*
Resets user(s) passwords
*/

pragma encoding, "UTF-8"
pragma runLim, 0
                
if (doorsname != (NLS_("Administrator"))) {
        warningBox((NLS_("You're not authorized to run this script!|\nPlease see you Administrator!")))
        halt
}

const string DOORS_DB = (NLS_("################"))

Buffer message = create(); message = (NLS_(""))
Buffer subject = create(); subject = (NLS_(""))

if (getDatabaseIdentifier() == DOORS_DB) {
        subject = (NLS_("DOORS_DB Rational DOORS Password Reset"))
} else {
        warningBox (NLS_("Unknown Database\n"))
        delete message
        delete subject
        halt
}

User uX
bool ugDisabled 
string ugName
string temp []
int userNum = 0
int listViewOptionNoMultiselect = 0
Buffer b = create()

DB db
DBE userListView

string getRandompw() {
        int i = 0
        int x = 0
        b = ""

        for (i= 0; i<32; i++) {
                while (true) {
                        x = random(123)
                        if ( x > 48 && x < 58 )  break //[0-9]
                        if ( x > 64 && x < 91 )  break //[a-z]
                        if ( x > 96 && x < 123 ) break //[A-Z]
                }
                b += charOf(x)
        }

        return(tempStringOf(b))
}

string changePassword(string cUserName, string cUserPassword)
// Tools > Options > Security > Change Password
{
        if (existsUser(cUserName)) {
                noError()
                        User u = find(cUserName)
                cUserName = lastError() 
                
                if (cUserName == null) {
                        if (loadUserRecord(u) == null) {
                                if (ensureUserRecordLoaded(u) == null) {             
                                        u.password = cUserPassword
                                        //sUserName = setUser(u, password, cUserPassword)
                                        if (saveUserRecord(u) == null) {
                                                // save new user list
                                                if (saveDirectory() == null) {
                                                        // inform user
                                                        cUserName = (NLS_("User was successfully updated"))
                                                } else {
                                                        // Error msg
                                                        cUserName = (NLS_("Failed to save user list"))
                                                }
                                        } else {
                                                // Error msg
                                                cUserName = (NLS_("Failed to save user record"))
                                        }
                                } else {
                                        // Error msg
                                        cUserName = (NLS_("Failed to load user record"))
                                }
                        } else {
                                // Error msg
                                cUserName = (NLS_("Failed to load user record"))
                        }
                } else {
                        // Error msg
                        cUserName = (NLS_("No such user"))
                }
        } else {
                // Error msg
                cUserName = (NLS_("No such user"))
        }
        return(cUserName)
}

void userSelected()
{
        int i            = 0
        string err       = null
        string cUserName = null
        string pw        = null
        string cpw       = null
                
        for (i = noElems (userListView); i >= 0; i--) {
                if (selected (userListView, i)) {
                        cUserName = get (userListView, i)
                        pw        = getRandompw()
                        cpw       = changePassword(cUserName, pw)

                        if (cpw == (NLS_("User was successfully updated"))) {
                                message = (NLS_("Username: ")) 
                                message += cUserName
                                message += (NLS_("\nPassword: ")) 
                                message += pw 
                                message += (NLS_("\n\nChange user's password:\nTools > Options > Security > Change Password"))

                                Skip targetAddress = create; put(targetAddress, 1, (username (NLS_("@email.com"))))
                                Skip ccAddresses = create; put(ccAddresses, 1, "admin@email.com")
                                err = sendEMailMessage(username (NLS_("@email.com")), targetAddress, ccAddresses, "subject", (tempStringOf(message)))
                                delete targetAddress
                                delete ccAddresses
                                
                                if (!null(err)) {
                                        errorBox(err)
                                } else {
                                        infoBox(NLS_("Email Sent"))
                                }
                        } else {
                                errorBox(cpw)
                        }
                }
        }
} // userSelected.

void applyAccess (DB win)
{
        userSelected()
} // applyAccess

int sortListView (string s1, string s2)
{ // Call back function to sort listViews.
        if (s1 == s2) {
                return 0
        }
        else if (s1 > s2) {
                return 1
        }
        else if (s1 < s2) {
                return -1
        }
} //sortListView.

void optionCallback (DBE win)
{
        userNum = 0
        
        //Empty the user list.
        empty (userListView)

        //Add the users to the user list view.
        for uX in userList do {
                ugDisabled = uX.disabled

                if (ugDisabled)
                        continue

                ugName = uX.name
                insert (userListView, userNum, ugName, iconUser)
                ugName = uX.fullName
                set (userListView, userNum, 1, ugName)
                userNum++
        }
        
        setSortColumn(userListView, 0)
} // optionCallback

//Start building the dialog box.
db = create ((NLS_("Password Reset...")), styleStandard|styleCentered)
userListView = listView (db, listViewOptionNoMultiselect, 320, 8, temp)

apply (db, (NLS_("Apply")), applyAccess)

realize(db)

//Put the appropriate columns in the list views.
insertColumn (userListView, 0, (NLS_("Username")), 200, iconNone)
insertColumn (userListView, 1, (NLS_("User")), 100, iconNone)

//Add the users to the user list view.
for uX in userList do {
        ugDisabled = uX.disabled
        
        if (ugDisabled) 
                continue
        
        ugName = uX.name
        insert (userListView, userNum, ugName, iconUser)
        ugName = uX.fullName
        set (userListView, userNum, 1, ugName)
        userNum++
}

//Set the sort on the list views.
set (userListView, 0, sortListView)
set (userListView, 1, sortListView)

setSortColumn(userListView, 0)

block(db)

destroy(db)
db = null

delete b
delete message
delete subject

 

Re: Creating a random string using regexp
MrNiceGuy - Wed Nov 29 11:18:27 EST 2017

EHcnck - Tue Nov 28 21:14:46 EST 2017

// User password reset

/*
Resets user(s) passwords
*/

pragma encoding, "UTF-8"
pragma runLim, 0
                
if (doorsname != (NLS_("Administrator"))) {
        warningBox((NLS_("You're not authorized to run this script!|\nPlease see you Administrator!")))
        halt
}

const string DOORS_DB = (NLS_("################"))

Buffer message = create(); message = (NLS_(""))
Buffer subject = create(); subject = (NLS_(""))

if (getDatabaseIdentifier() == DOORS_DB) {
        subject = (NLS_("DOORS_DB Rational DOORS Password Reset"))
} else {
        warningBox (NLS_("Unknown Database\n"))
        delete message
        delete subject
        halt
}

User uX
bool ugDisabled 
string ugName
string temp []
int userNum = 0
int listViewOptionNoMultiselect = 0
Buffer b = create()

DB db
DBE userListView

string getRandompw() {
        int i = 0
        int x = 0
        b = ""

        for (i= 0; i<32; i++) {
                while (true) {
                        x = random(123)
                        if ( x > 48 && x < 58 )  break //[0-9]
                        if ( x > 64 && x < 91 )  break //[a-z]
                        if ( x > 96 && x < 123 ) break //[A-Z]
                }
                b += charOf(x)
        }

        return(tempStringOf(b))
}

string changePassword(string cUserName, string cUserPassword)
// Tools > Options > Security > Change Password
{
        if (existsUser(cUserName)) {
                noError()
                        User u = find(cUserName)
                cUserName = lastError() 
                
                if (cUserName == null) {
                        if (loadUserRecord(u) == null) {
                                if (ensureUserRecordLoaded(u) == null) {             
                                        u.password = cUserPassword
                                        //sUserName = setUser(u, password, cUserPassword)
                                        if (saveUserRecord(u) == null) {
                                                // save new user list
                                                if (saveDirectory() == null) {
                                                        // inform user
                                                        cUserName = (NLS_("User was successfully updated"))
                                                } else {
                                                        // Error msg
                                                        cUserName = (NLS_("Failed to save user list"))
                                                }
                                        } else {
                                                // Error msg
                                                cUserName = (NLS_("Failed to save user record"))
                                        }
                                } else {
                                        // Error msg
                                        cUserName = (NLS_("Failed to load user record"))
                                }
                        } else {
                                // Error msg
                                cUserName = (NLS_("Failed to load user record"))
                        }
                } else {
                        // Error msg
                        cUserName = (NLS_("No such user"))
                }
        } else {
                // Error msg
                cUserName = (NLS_("No such user"))
        }
        return(cUserName)
}

void userSelected()
{
        int i            = 0
        string err       = null
        string cUserName = null
        string pw        = null
        string cpw       = null
                
        for (i = noElems (userListView); i >= 0; i--) {
                if (selected (userListView, i)) {
                        cUserName = get (userListView, i)
                        pw        = getRandompw()
                        cpw       = changePassword(cUserName, pw)

                        if (cpw == (NLS_("User was successfully updated"))) {
                                message = (NLS_("Username: ")) 
                                message += cUserName
                                message += (NLS_("\nPassword: ")) 
                                message += pw 
                                message += (NLS_("\n\nChange user's password:\nTools > Options > Security > Change Password"))

                                Skip targetAddress = create; put(targetAddress, 1, (username (NLS_("@email.com"))))
                                Skip ccAddresses = create; put(ccAddresses, 1, "admin@email.com")
                                err = sendEMailMessage(username (NLS_("@email.com")), targetAddress, ccAddresses, "subject", (tempStringOf(message)))
                                delete targetAddress
                                delete ccAddresses
                                
                                if (!null(err)) {
                                        errorBox(err)
                                } else {
                                        infoBox(NLS_("Email Sent"))
                                }
                        } else {
                                errorBox(cpw)
                        }
                }
        }
} // userSelected.

void applyAccess (DB win)
{
        userSelected()
} // applyAccess

int sortListView (string s1, string s2)
{ // Call back function to sort listViews.
        if (s1 == s2) {
                return 0
        }
        else if (s1 > s2) {
                return 1
        }
        else if (s1 < s2) {
                return -1
        }
} //sortListView.

void optionCallback (DBE win)
{
        userNum = 0
        
        //Empty the user list.
        empty (userListView)

        //Add the users to the user list view.
        for uX in userList do {
                ugDisabled = uX.disabled

                if (ugDisabled)
                        continue

                ugName = uX.name
                insert (userListView, userNum, ugName, iconUser)
                ugName = uX.fullName
                set (userListView, userNum, 1, ugName)
                userNum++
        }
        
        setSortColumn(userListView, 0)
} // optionCallback

//Start building the dialog box.
db = create ((NLS_("Password Reset...")), styleStandard|styleCentered)
userListView = listView (db, listViewOptionNoMultiselect, 320, 8, temp)

apply (db, (NLS_("Apply")), applyAccess)

realize(db)

//Put the appropriate columns in the list views.
insertColumn (userListView, 0, (NLS_("Username")), 200, iconNone)
insertColumn (userListView, 1, (NLS_("User")), 100, iconNone)

//Add the users to the user list view.
for uX in userList do {
        ugDisabled = uX.disabled
        
        if (ugDisabled) 
                continue
        
        ugName = uX.name
        insert (userListView, userNum, ugName, iconUser)
        ugName = uX.fullName
        set (userListView, userNum, 1, ugName)
        userNum++
}

//Set the sort on the list views.
set (userListView, 0, sortListView)
set (userListView, 1, sortListView)

setSortColumn(userListView, 0)

block(db)

destroy(db)
db = null

delete b
delete message
delete subject

 

What script/program is this for?

I tried to get it to work last night but im not very familiar with NLS_ and its function.

I suppose I am meant to replace the ###### with my DB name?

because if I comment out lines 15 and lines 20 - 27 I can get it to work, I just wanted to know what the importance of those lines of code are used for?

Re: Creating a random string using regexp
DOORSguy - Thu Nov 30 10:27:23 EST 2017

MrNiceGuy - Wed Nov 29 11:18:27 EST 2017

What script/program is this for?

I tried to get it to work last night but im not very familiar with NLS_ and its function.

I suppose I am meant to replace the ###### with my DB name?

because if I comment out lines 15 and lines 20 - 27 I can get it to work, I just wanted to know what the importance of those lines of code are used for?

Regarding NLS_, in brief, it simply returns the parameter that is passed to it. While reading code, you can almost always ignore the function and just focus on the argument.

For more details see this thread: New DOORS 9.2 Undocumented DXL Functions

 

The ###### would need to be replaced with your database identifier, a 16-character string that can be seen with:

string s = getDatabaseIdentifier()
print s

You would want any copy of this script to be "keyed" to a specific database, because the "email.com" instances would need to be customized for the specific database where the script is used. Those lines of code check that the script is being run in the database for which it was customized.

 

Bill